﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class DistributionRangeList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnEdit;
        protected Button btnReSearch;
        protected HtmlForm F2572;
        protected GridView gvDistributionRange;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblDistributionRangeList;
        protected Label lblMsg;
        protected LinkButton lnkbtnImportData;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnImportData;
        protected UpdatePanel up1;

        private void BindDataDistributionRangeList(DistributionRangeQueryEntity DistributionRangeQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(DistributionRangeQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(DistributionRangeQueryEntity, "DISTRIBUTION_RANGE");
            base.BindData2GridView(this.gvDistributionRange, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvDistributionRange, "ROWID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项进行编辑";
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = "只能选择一项进行编辑";
            }
            else
            {
                base.Response.Redirect(string.Format("DistributionRange.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            DistributionRangeQueryEntity DistributionRangeQueryEntity = this.GetDistributionRangeQueryEntity();
            DistributionRangeQueryEntity.CurrentPage = "0";
            this.BindDataDistributionRangeList(DistributionRangeQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            DistributionRangeQueryEntity DistributionRangeQueryEntity = this.GetDistributionRangeQueryEntity();
            DistributionRangeQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataDistributionRangeList(DistributionRangeQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            DistributionRangeQueryEntity DistributionRangeQueryEntity = this.GetDistributionRangeQueryEntity();
            if (int.Parse(DistributionRangeQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                DistributionRangeQueryEntity.CurrentPage = (int.Parse(DistributionRangeQueryEntity.CurrentPage) + 1).ToString();
                this.BindDataDistributionRangeList(DistributionRangeQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            DistributionRangeQueryEntity DistributionRangeQueryEntity = this.GetDistributionRangeQueryEntity();
            if (int.Parse(DistributionRangeQueryEntity.CurrentPage) > 0)
            {
                DistributionRangeQueryEntity.CurrentPage = (int.Parse(DistributionRangeQueryEntity.CurrentPage) - 1).ToString();
                this.BindDataDistributionRangeList(DistributionRangeQueryEntity);
            }
        }

        private DistributionRangeQueryEntity GetDistributionRangeQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as DistributionRangeQueryEntity);
            }
            return (base.PageQueryEntitySession as DistributionRangeQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                DistributionRangeQueryEntity DistributionRangeQueryEntity = this.GetDistributionRangeQueryEntity();
                DistributionRangeQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataDistributionRangeList(DistributionRangeQueryEntity);
            }
        }

        protected void gvDistributionRange_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2572", this.gvDistributionRange);
        }

        protected void gvDistributionRange_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvDistributionRange_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DistributionRangeQueryEntity distributionRangeQueryEntity = this.GetDistributionRangeQueryEntity();
                distributionRangeQueryEntity.SortField = e.SortExpression;
                if (distributionRangeQueryEntity.SortDirection == "ASC")
                {
                    distributionRangeQueryEntity.SortDirection = "DESC";
                }
                else if (distributionRangeQueryEntity.SortDirection == "DESC")
                {
                    distributionRangeQueryEntity.SortDirection = "ASC";
                }
                this.BindDataDistributionRangeList(distributionRangeQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            DistributionRangeQueryEntity DistributionRangeQueryEntity = new DistributionRangeQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    DistributionRangeQueryEntity.Province = this.PreviousPage.Province;
                    DistributionRangeQueryEntity.City = this.PreviousPage.City;
                    DistributionRangeQueryEntity.CarrierID = this.PreviousPage.CarrierID;
                    DistributionRangeQueryEntity.Counties = this.PreviousPage.Counties;
                }
                else
                {
                    DistributionRangeQueryEntity.Province = "";
                    DistributionRangeQueryEntity.City = "";
                    DistributionRangeQueryEntity.CarrierID = "";
                    DistributionRangeQueryEntity.Counties = "";
                }
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"];
            }
            DistributionRangeQueryEntity.IsGetAll = false;
            DistributionRangeQueryEntity.CurrentPage = "0";
            DistributionRangeQueryEntity.PageSize = this.gvDistributionRange.PageSize.ToString();
            DistributionRangeQueryEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvDistributionRange.PageSize;
            this.SetDistributionRangeQueryEntity(DistributionRangeQueryEntity);
            this.BindDataDistributionRangeList(DistributionRangeQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetDistributionRangeQueryEntity(DistributionRangeQueryEntity DistributionRangeQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = DistributionRangeQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = DistributionRangeQueryEntity;
            }
        }

        public DistributionRangeQuery PreviousPage
        {
            get
            {
                return (DistributionRangeQuery) base.PreviousPage;
            }
        }
    }
}

